<template>
  <el-container class="layout-container">
    <el-aside width="220px" class="layout-aside">
      <div class="logo">
        <span>管理系统</span>
      </div>
      <el-menu :router="true" class="layout-menu" background-color="#304156" text-color="#bfcbd9" active-text-color="#409EFF">
        <el-submenu index="1">
          <template #title>
            <i class="el-icon-setting" />
            <span>系统管理</span>
          </template>
          <el-menu-item index="/users" v-if="hasPermission('/system/user/list')">
            <i class="el-icon-user" />
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="/roles" v-if="hasPermission('/system/role/list')">
            <i class="el-icon-s-check" />
            <span>角色管理</span>
          </el-menu-item>
          <el-menu-item index="/resources" v-if="hasPermission('/system/resources/list')">
            <i class="el-icon-menu" />
            <span>权限管理</span>
          </el-menu-item>
        </el-submenu>
      </el-menu>
    </el-aside>
    <el-container class="layout-main">
      <el-header class="layout-header">
        <div class="header-left">
          <i class="el-icon-s-fold" @click="toggleSidebar" />
          <breadcrumb />
        </div>
        <div class="header-right">
          <el-dropdown>
            <span class="el-dropdown-link">
              管理员 <i class="el-icon-arrow-down el-icon--right" />
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="changePassword">修改密码</el-dropdown-item>
                <el-dropdown-item @click="logout">退出系统</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-main class="layout-content">
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<script>

import { hasPermission } from '@/utils/permissions'


export default {
  name: 'LayoutComponent',
  methods: {
    toggleSidebar() {
      // 实现侧边栏折叠功能
    },
    changePassword() {
      this.$router.push('/update-password')

    },
    logout() {
      this.$store.dispatch('logout')
      this.$router.push('/login')
    },
    hasPermission(permission) {
      return hasPermission(this.$store, permission);
    }
  }
}
</script>

<style scoped>
.layout-container {
  height: 100vh;
}

.layout-aside {
  background-color: #304156;
  color: #bfcbd9;
}

.logo {
  height: 60px;
  display: flex;
  align-items: center;
  padding-left: 20px;
  background-color: #2b2f3a;
}

.logo img {
  width: 32px;
  height: 32px;
  margin-right: 12px;
}

.layout-menu {
  border-right: none;
}

.layout-header {
  background-color: #fff;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-left i {
  font-size: 20px;
  cursor: pointer;
  margin-right: 15px;
}

.layout-content {
  padding: 20px;
  background-color: #f0f2f5;
}
</style>